﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Generator
{
    public partial class Form1 : Form
    {
        private Gramatica gramatica;

        public Form1()
        {
            InitializeComponent();
        }

        private void clkDeschideFisierIntrare(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                string numeFisier=null;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    numeFisier = ofd.FileName;
                }

                if (File.Exists(@numeFisier) == false)
                {
                    MessageBox.Show("Numele fisierului este gresit.","",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                string[] liniiFisier = File.ReadAllLines(@numeFisier);
                string SimbolStart = liniiFisier[0];
                List<string> Neterminale = liniiFisier[1].Split(',').ToList();
                List<Tokens> Terminale = new List<Tokens>();
                foreach (var t in liniiFisier[2].Split(',').ToList())
                    switch (t)
                    {
                        case "KEYWORD":
                            Terminale.Add(Tokens.KEYWORD);break;
                        case "IDENTIFIER":
                            Terminale.Add(Tokens.IDENTIFIER); break;
                        case "INTEGER_LITERAL":
                            Terminale.Add(Tokens.INTEGER_LITERAL); break;
                        case "REAL_LITERAL":
                            Terminale.Add(Tokens.REAL_LITERAL); break;
                        case "OPERATOR":
                            Terminale.Add(Tokens.OPERATOR); break;
                        case "PUNCTUATOR":
                            Terminale.Add(Tokens.PUNCTUATOR); break;
                        default:
                            throw new Exception("Atom lexical nedefinit pentru analizorul lexical.");
                    }
                Terminale.Add(Tokens.ERROR);
                Terminale.Add(Tokens.DOLLAR);

                int nrReguli = int.Parse(liniiFisier[3]);
                List<RegulaProductie> reguliProductie = new List<RegulaProductie>();
                string regula;
                for (int i = 0; i < nrReguli; i++)
                {
                    regula = liniiFisier[i + 4];
                    string Neterminal=null;
                    StringBuilder sb1=new StringBuilder();
                    int j=0;
                    for(;j<regula.Length;j++)
                        if(regula[j]=='-')
                            break;
                        else
                            sb1.Append(regula[j]);
                    Neterminal=sb1.ToString();

                    StringBuilder sb=new StringBuilder();
                    List<NeterminalSauTerminal> l=new List<NeterminalSauTerminal>();
                    for (j = j + 2; j < regula.Length; )
                    {
                        while (j < regula.Length)
                        {
                            if(regula[j] == ' ')
                                break;
                            sb.Append(regula[j]);
                            j++;
                        }

                        j++;

                        bool esteTerminal = false;
                        if (sb.ToString() == "epsilon")
                            l.Add(new NeterminalSauTerminal() { esteNeterminal = false, esteTerminal = false, val = sb.ToString() });
                        else
                        {
                            foreach (var t in Terminale)
                                if (t.ToString() == sb.ToString())
                                {
                                    esteTerminal = true;
                                    l.Add(new NeterminalSauTerminal() { esteNeterminal = false, esteTerminal = true, val = sb.ToString() });
                                    break;
                                }
                            if (esteTerminal == false)
                                l.Add(new NeterminalSauTerminal() { esteNeterminal = true, esteTerminal = false, val = sb.ToString() });
                        }

                        sb.Clear();
                    }

                    reguliProductie.Add(new RegulaProductie(i,Neterminal,l));
                }

                gramatica = new Gramatica(SimbolStart,Neterminale,Terminale,nrReguli,reguliProductie);

                string dovada;
                if (gramatica.esteLL1(out dovada) == true)
                {
                    MessageBox.Show("Gramatica este LL1.");
                    MessageBox.Show(dovada, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    gramatica.genereazaCod();
                    dataGridView1.DataSource = gramatica.daTabela();
                    btnGenereaza.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Gramatica nu este LL1.");
                    btnGenereaza.Enabled = false;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());             
            }
        }

        private void clkIesi(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clkGenereaza(object sender, EventArgs e)
        {
            gramatica.executaCod();
        }
    }
}
